#include <cmath>
#include <assert.h>
#include "cvector.h"

using namespace spacedefender;


Vector::Vector()
{
    i=0.0; j=0.0; k=0.0;
}


Vector::Vector(double i, double j, double k)
{
    this->i=i; this->j=j; this->k=k;
}

Vector::Vector(float i, float j, float k)
{
    this->i=i; this->j=j; this->k=k;
}



double Vector::length() const
{
    return (double) sqrt (i*i + j*j + k*k) ;
}


Vector Vector::operator* (double scalar) const
{
    return Vector( i*scalar, j*scalar, k*scalar );
}


inline Vector Vector::operator* (const Vector &ve) const
{
    return Vector(i*ve.i , j*ve.j , k*ve.k) ;
}


inline Vector Vector::operator/ (const Vector &ve) const
{
    return Vector(i/ve.i , j/ve.j , k/ve.k) ;
}


Vector Vector::operator- (const Vector &ve) const
{
    return Vector(i-ve.i , j-ve.j , k-ve.k);
}



Vector Vector::operator+ (const Vector &ve) const
{
    return Vector(i+ve.i , j+ve.j , k+ve.k);
}


Vector Vector::crossProduct(const Vector &v1, const Vector &v2)
{
    return Vector((v1.j*v2.k)-(v1.k * v2.j),(v1.k*v2.i)-(v1.i*v2.k),(v1.i*v2.j)-(v1.j*v2.i));
}


double Vector::distance(const Vector &v1, const Vector &v2)
{
    return sqrt(((v1.i-v2.i)*(v1.i-v2.i)+(v1.j-v2.j)*(v1.j-v2.j)+(v1.k-v2.k)*(v1.k-v2.k)));
}

Vector Vector::DistanceVector(const Vector &v1, const Vector &v2)
{
    return Vector(v2-v1);
}



double  Vector::dotProduct(const Vector &v1, const Vector &v2)
{
    return (v1.i*v2.i + v1.j*v2.j + v1.k*v2.k);
}


//  pre:  length() != 0
// post:  length() == 1
void Vector::normalize()
{
    double len = length();
    assert(len != 0.0 ) ;
    i = i/len;
    j = j/len;
    k = k/len;
}

std::string Vector::ToString() const
{
	char txt[100];

	#ifdef _WIN32
	    _snprintf_s(txt,100,"i=%f j=%f k=%f", i,j,k);
	#else
	    snprintf(txt,100,"i=%f j=%f k=%f", i,j,k);
	#endif

	return std::string(txt) ;
}

